<?php
namespace Common\Model;
use Hdphp\Model\Model;
class User extends Model {
	protected $table = 'user';
	protected $validate = array(
	//		array('telphone','phone','手机号格式错误',3,3),

	);
	public function store() {
		if (!$this -> create())
			return false;
		if ($this -> error)
			return false;
		p($_POST);
		if ($_POST['password'] != $_POST['rePass']) {
			$this -> error = '两次输入密码不一样';
		}
		$len = count($_POST['password']);
		if ($len < 6 || $len > 11) {
			$this -> error = "密码要求6~11位";
		}
		$this -> data['password'] = md5($_POST['password']);
		//		p($this->data);die;
		$this -> add($this -> data);
		return true;

	}

	public function login() {
		if (!$this -> create())
			return false;
		if ($this -> error)
			return false;
		$data = $this -> get();
		//		p($data);die;
		foreach ($data as $k => $v) {
			if (!md5($_POST['password']) == $v['password']) {
				$this -> error = "用户名或密码错误";
				return false;
			}

			if ($_POST['username'] == $v['username']) {
				$uid = $this -> where(array('username' => $_POST['username'])) -> pluck('uid');
				$_SESSION['user'] = array('username' => $_POST['username'], 'uid' => $v['uid']);
				if (strtoupper($_POST['code']) != $_SESSION['code']) {
					$this -> error = '验证码错误';
					return false;
				}
				return true;
			} else {
				$this -> error = "用户名或密码错误";
				return false;
			}

			if ($_POST['username'] == $v['phone']) {
				$uid = $this -> where(array('phone' => $_POST['username'])) -> pluck('uid');
				$_SESSION['user'] = array('username' => $_POST['username'], 'uid' => $v['uid']);
				if (strtoupper($_POST['code']) != $_SESSION['code']) {
					$this -> error = '验证码错误';
					return false;
				}
				return true;

			} else {
				$this -> error = "用户名或密码错误";
				return false;
			}

			return false;

		}

	}

}
?>
